﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JukeBox.Properties;
using JukeBox.Columns;

namespace JukeBox
{
	delegate void SetUpdateProgressBarCallback(int value);
    public partial class MainForms : Form
    {
        private JukeBoxLib.Tracks _tracks;
        private Color _currentrowcolor = Color.White;
        private bool _isscanning = false;
        private JukeBoxLib.Scanner _scanner = null;


        public MainForms()
        {
            InitializeComponent();
        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbdPath = new FolderBrowserDialog();
            if(!string.IsNullOrWhiteSpace(textBoxPath.Text))
                fbdPath.SelectedPath = textBoxPath.Text;
            fbdPath.ShowDialog();
            if(!string.IsNullOrWhiteSpace(fbdPath.SelectedPath))
                textBoxPath.Text = fbdPath.SelectedPath;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadSetting();
            UpdateColumns();
            UpdateView();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_isscanning && _scanner !=null)
                _scanner.StopScanning();
            SaveSetting();
        }

        private void buttonScan_Click(object sender, EventArgs e)
        {
            if (!_isscanning)
            {
                _isscanning = true;
                _scanner = new JukeBoxLib.Scanner(textBoxPath.Text);
                _tracks = _scanner.Tracks;

                progressBarScanning.Maximum = _scanner.Count;

                _scanner.UpdateProgress += new JukeBoxLib.UpdateProgressDelegate(scanner_UpdateProgress);
                _scanner.EndScan += new JukeBoxLib.EndScanDelegate(scanner_EndScan);

                buttonScan.Text = "Stop";
            }
            else
            {
                _scanner.StopScanning();
                _isscanning = false;
                buttonScan.Text = "Start...";
                progressBarScanning.Value = 0;
            }
        }

        void scanner_EndScan()
        {
            if (this.listViewTracks.InvokeRequired)
            {
                JukeBoxLib.EndScanDelegate endscan = new JukeBoxLib.EndScanDelegate(scanner_EndScan);
                this.Invoke(endscan);
            }
            else
            {
                _isscanning = false;
                progressBarScanning.Value = 0;
                buttonScan.Text = "Start...";
                UpdateView();
            }
        }

        void scanner_UpdateProgress(int progress)
        {
            if (this.progressBarScanning.InvokeRequired)
            {
                //SetUpdateProgressBarCallback d = new SetUpdateProgressBarCallback(scanner_UpdateProgress);
                JukeBoxLib.UpdateProgressDelegate d = new JukeBoxLib.UpdateProgressDelegate(scanner_UpdateProgress);
                this.Invoke(d, new object[] { progress });
            }
            else
            {
                this.progressBarScanning.Value = progress;
            }
        }

        private void UpdateView()
        {
            listViewTracks.Items.Clear();
            if (_tracks == null)
                return;
            foreach (JukeBoxLib.Track t in _tracks.Items)
            {
                AddToList(t);
                foreach (JukeBoxLib.Track td in t.Duplicates)
                {
                    AddToList(td, false);
                }
            }
        }

        private void UpdateColumns()
        {
            listViewTracks.Columns.Clear();
            foreach (ListColumn Col in ColumnsCollection.Columns)
                listViewTracks.Columns.Add(Col.Name, Col.Width);
        }

        private void AddToList(JukeBoxLib.Track track, bool changecolor = true)
        {
            if (_currentrowcolor == Color.Azure && changecolor)
                _currentrowcolor = Color.White;
            else
                _currentrowcolor = Color.Azure;

            List<string> Params = new List<string>();
            foreach (ListColumn var in ColumnsCollection.Columns)
                Params.Add(var.GetValue(track));

            ListViewItem LVI = new ListViewItem(Params.ToArray());
            LVI.Name = track.FileName;
            LVI.Tag = track;
            LVI.BackColor = _currentrowcolor;
            listViewTracks.Items.Add(LVI);
        }

        private void LoadSetting()
        {
            //this.WindowState = Settings.Default.MainFormMaximized ? FormWindowState.Maximized : FormWindowState.Normal;
            //if (this.WindowState != FormWindowState.Maximized)
            //{
            //    this.StartPosition = FormStartPosition.Manual;
            //    //this.Size = Settings.Default.MainFormSize;
            //    //this.Location = Settings.Default.MainFormLocation;
            //}

            textBoxPath.Text = Settings.Default.Path;
            //mnuViewLeftPanel.Checked = Settings.Default.ShowLeftPanel;
            //mnuViewProffesionalPanel.Checked = Settings.Default.ShowProfessionalPanel;
            //pnlLeft.Size = Settings.Default.LeftPanelSize;

            //tbcRight.SelectedIndex = (Settings.Default.SelectedTab < tbcRight.TabPages.Count) ?
            //  Settings.Default.SelectedTab : 2;
        }

        private void SaveSetting()
        {
            for (int i = 0; i < listViewTracks.Columns.Count; i++)
                ColumnsCollection.Columns[i].Width = listViewTracks.Columns[i].Width;
            ColumnsCollection.Save();


            //Settings.Default.MainFormMaximized = (this.WindowState == FormWindowState.Maximized);
            if (this.WindowState != FormWindowState.Maximized)
            {
                //Settings.Default.MainFormSize = this.Size;
                //Settings.Default.MainFormLocation = this.Location;
            }

            Settings.Default.Path = textBoxPath.Text;
            //Settings.Default.ShowLeftPanel = mnuViewLeftPanel.Checked;
            //Settings.Default.ShowProfessionalPanel = mnuViewProffesionalPanel.Checked;
            //Settings.Default.LeftPanelSize = pnlLeft.Size;
            //Settings.Default.SelectedTab = tbcRight.SelectedIndex;

            Settings.Default.Save();
        }

        private void listColumnsToolStripMenuItem3_Click(object sender, EventArgs e)
        {
            frmColumnSetting ColForm = new frmColumnSetting();
            if (ColForm.ShowDialog() == DialogResult.OK)
            {
                UpdateColumns();
                UpdateView();
            }
        }
    }
}
